package com.caits.lbs.framework.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import org.apache.log4j.Logger;

import com.caits.lbs.framework.bean.common.TimeToolsBean;
import com.caits.lbs.framework.log.CommonLogFactory;

/**
 * <p>
 * TimeTools
 * </p>
 * <p>
 * 用途：日期时间工具类
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.1 2011-7-1
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-7-1 下午3:35:11</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.1</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-7-1 下午3:35:11</td>
 *          <td>0.0.2</td>
 *          <td>修改类</td>
 *          <td>xxx</td>
 *          <td>x年x月x日</td>
 *          </tr>
 *          </table>
 */
public class TimeTools {

	/** 1小时的分钟数 */
	public static final int ONE_HOUR_MINUTES = 60;
	
	/** 日志记录器 */
	private static Logger log = CommonLogFactory.getLog();
	/** 日期格式化器 */
	static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

	/** 小时描述格式 */
	static Map<String, String> hourdesc = new HashMap<String, String>() {
		/** TODO */
		private static final long serialVersionUID = 1L;

		{
			put("00", "00:00--01:00");
			put("01", "01:00--02:00");

			put("02", "02:00--03:00");
			put("03", "03:00--04:00");

			put("04", "04:00--05:00");
			put("05", "05:00--06:00");

			put("06", "06:00--07:00");
			put("07", "07:00--08:00");

			put("08", "08:00--09:00");
			put("09", "09:00--10:00");

			put("10", "10:00--11:00");
			put("11", "11:00--12:00");

			put("12", "12:00--13:00");
			put("13", "13:00--14:00");

			put("14", "14:00--15:00");
			put("15", "15:00--16:00");

			put("16", "16:00--17:00");
			put("17", "17:00--18:00");

			put("18", "18:00--19:00");
			put("19", "19:00--20:00");

			put("20", "20:00--21:00");
			put("21", "21:00--22:00");

			put("22", "22:00--23:00");
			put("23", "23:00--00:00");
		}
	};

	/**
	 * 时间段处理
	 * 
	 * @param stattime
	 * @return
	 */
	public static TimeToolsBean convertToPartTime(String stattime) {
		TimeToolsBean timeToolsBean = new TimeToolsBean();
		int length = stattime.trim().length();
		timeToolsBean.setStattimelength(length);
		try {
			String[] stattimes = stattime.split("-");
			if (length == 4) {// 年
				timeToolsBean.setNowstarttime(formatter.parse(
						stattime + "-01-01").getTime() / 1000);
				timeToolsBean.setNowendtime(formatter
						.parse(stattime + "-12-31").getTime() / 1000);
				timeToolsBean.setStattimedesc(stattime + "年");
			} else if (length == 6) {// 季

				int quarter = Integer.parseInt(stattimes[1]);
				timeToolsBean.setStattimedesc(stattimes[0] + "年第"
						+ stattimes[1] + "季度");
				if (quarter == 1) {
					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-01-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-03-31").getTime() / 1000);
				} else if (quarter == 2) {

					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-04-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-06-30").getTime() / 1000);
				} else if (quarter == 3) {

					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-07-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-09-30").getTime() / 1000);
				} else if (quarter == 4) {

					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-10-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-12-31").getTime() / 1000);
				}
			} else if (length == 7) {// 月

				Date date = formatter.parse(stattime + "-01");
				timeToolsBean.setNowstarttime(date.getTime() / 1000);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.MONTH, 1);
				calendar.add(Calendar.DAY_OF_YEAR, -1);

				timeToolsBean.setNowendtime(calendar.getTimeInMillis() / 1000);
				timeToolsBean.setStattimedesc(stattimes[0] + "年" + stattimes[1]
						+ "月");

			} else if (length == 9) {// 周

				int week = Integer.parseInt(stattimes[2]);
				Calendar calendar = Calendar.getInstance();
				calendar.set(Integer.parseInt(stattimes[0]),
						Integer.parseInt(stattimes[1]) - 1, 1);

				calendar.add(Calendar.WEEK_OF_MONTH, week - 1);
				timeToolsBean.setNowstarttime(formatter.parse(
						formatter.format(calendar.getTime())).getTime() / 1000);
				timeToolsBean
						.setNowendtime(timeToolsBean.getNowstarttime() + 604800);
				timeToolsBean.setStattimedesc(stattimes[0] + "年" + stattimes[1]
						+ "月第" + stattimes[2] + "周");

			} else if (length == 10) {// 日

				timeToolsBean.setNowstarttime(formatter.parse(stattime)
						.getTime() / 1000);
				timeToolsBean
						.setNowendtime(timeToolsBean.getNowstarttime() + 86400);
				timeToolsBean.setStattimedesc(stattimes[0] + "年" + stattimes[1]
						+ "月" + stattimes[2] + "日");
			}

			return timeToolsBean;

		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 环比时间处理
	 * 
	 * @param stattime
	 * @return
	 */
	public static TimeToolsBean convertToLRRTime(String stattime) {

		TimeToolsBean timeToolsBean = new TimeToolsBean();
		int length = stattime.trim().length();
		timeToolsBean.setStattimelength(length);
		try {
			String[] stattimes = stattime.split("-");

			if (length == 4) {// 年
				String uptime = String.valueOf(Integer.parseInt(stattime) - 1);
				timeToolsBean.setBeforestarttime(formatter.parse(
						uptime + "-01-01").getTime() / 1000);
				timeToolsBean.setBeforeendtime(formatter.parse(
						uptime + "-12-31").getTime() / 1000);
				timeToolsBean.setNowstarttime(formatter.parse(
						stattime + "-01-01").getTime() / 1000);
				timeToolsBean.setNowendtime(formatter
						.parse(stattime + "-12-31").getTime() / 1000);
				timeToolsBean.setStattimedesc(stattime + "年");
			} else if (length == 6) {// 季
				int quarter = Integer.parseInt(stattimes[1]);
				timeToolsBean.setStattimedesc(stattimes[0] + "年第"
						+ stattimes[1] + "季度");

				if (quarter == 1) {
					String uptime = String.valueOf(Integer
							.parseInt(stattimes[0]) - 1);

					timeToolsBean.setBeforestarttime(formatter.parse(
							uptime + "-10-01").getTime() / 1000);
					timeToolsBean.setBeforeendtime(formatter.parse(
							uptime + "-12-31").getTime() / 1000);
					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-01-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-03-31").getTime() / 1000);

				} else if (quarter == 2) {

					timeToolsBean.setBeforestarttime(formatter.parse(
							stattimes[0] + "-01-01").getTime() / 1000);
					timeToolsBean.setBeforeendtime(formatter.parse(
							stattimes[0] + "-03-31").getTime() / 1000);
					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-04-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-06-30").getTime() / 1000);

				} else if (quarter == 3) {

					timeToolsBean.setBeforestarttime(formatter.parse(
							stattimes[0] + "-04-01").getTime() / 1000);
					timeToolsBean.setBeforeendtime(formatter.parse(
							stattimes[0] + "-06-30").getTime() / 1000);
					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-07-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-09-30").getTime() / 1000);

				} else if (quarter == 4) {

					timeToolsBean.setBeforestarttime(formatter.parse(
							stattimes[0] + "-07-01").getTime() / 1000);
					timeToolsBean.setBeforeendtime(formatter.parse(
							stattimes[0] + "-09-30").getTime() / 1000);
					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-10-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-12-31").getTime() / 1000);

				}
			} else if (length == 7) {// 月
				Date date = formatter.parse(stattime + "-01");

				timeToolsBean.setNowstarttime(date.getTime() / 1000);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.MONTH, 1);
				calendar.add(Calendar.DAY_OF_YEAR, -1);

				timeToolsBean.setNowendtime(calendar.getTimeInMillis() / 1000);
				calendar.add(Calendar.MONTH, -1);
				calendar.set(Calendar.DATE,
						calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

				timeToolsBean
						.setBeforeendtime(calendar.getTimeInMillis() / 1000);

				calendar.add(Calendar.DAY_OF_YEAR, 1);
				calendar.add(Calendar.MONTH, -1);

				timeToolsBean
						.setBeforestarttime(calendar.getTimeInMillis() / 1000);

				timeToolsBean.setStattimedesc(stattimes[0] + "年" + stattimes[1]
						+ "月");
			} else if (length == 9) {// 周

				int week = Integer.parseInt(stattimes[2]);
				Calendar calendar = Calendar.getInstance();
				calendar.set(Integer.parseInt(stattimes[0]),
						Integer.parseInt(stattimes[1]) - 1, 1);

				calendar.add(Calendar.WEEK_OF_MONTH, week - 1);
				timeToolsBean.setBeforeendtime(formatter.parse(
						formatter.format(calendar.getTime())).getTime() / 1000);
				timeToolsBean.setBeforestarttime(timeToolsBean
						.getBeforeendtime() - 604800);
				timeToolsBean.setNowstarttime(timeToolsBean.getBeforeendtime());
				timeToolsBean
						.setNowendtime(timeToolsBean.getBeforeendtime() + 604800);
				timeToolsBean.setStattimedesc(stattimes[0] + "年" + stattimes[1]
						+ "月第" + stattimes[2] + "周");
			} else if (length == 10) {// 日
				timeToolsBean.setBeforeendtime(formatter.parse(stattime)
						.getTime() / 1000);
				timeToolsBean.setBeforestarttime(timeToolsBean
						.getBeforeendtime() - 86400);
				timeToolsBean.setNowstarttime(timeToolsBean.getBeforeendtime());
				timeToolsBean
						.setNowendtime(timeToolsBean.getBeforeendtime() + 86400);

				timeToolsBean.setStattimedesc(stattimes[0] + "年" + stattimes[1]
						+ "月" + stattimes[2] + "日");
			}

			return timeToolsBean;

		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 同比时间处理
	 * 
	 * @param stattime
	 * @return
	 */
	public static TimeToolsBean convertToYOYTime(String stattime) {

		TimeToolsBean timeToolsBean = new TimeToolsBean();
		int length = stattime.trim().length();
		timeToolsBean.setStattimelength(length);
		try {
			String[] stattimes = stattime.split("-");
			if (length == 4) {// 年
				String uptime = String.valueOf(Integer.parseInt(stattime) - 1);
				timeToolsBean.setBeforestarttime(formatter.parse(
						uptime + "-01-01").getTime() / 1000);
				timeToolsBean.setBeforeendtime(formatter.parse(
						uptime + "-12-31").getTime() / 1000);
				timeToolsBean.setNowstarttime(formatter.parse(
						stattime + "-01-01").getTime() / 1000);
				timeToolsBean.setNowendtime(formatter
						.parse(stattime + "-12-31").getTime() / 1000);
				timeToolsBean.setStattimedesc(stattime + "年");
			} else if (length == 6) {// 季

				String uptime = String
						.valueOf(Integer.parseInt(stattimes[0]) - 1);
				int quarter = Integer.parseInt(stattimes[1]);
				timeToolsBean.setStattimedesc(stattimes[0] + "年第"
						+ stattimes[1] + "季度");
				if (quarter == 1) {
					timeToolsBean.setBeforestarttime(formatter.parse(
							uptime + "-01-01").getTime() / 1000);
					timeToolsBean.setBeforeendtime(formatter.parse(
							uptime + "-03-31").getTime() / 1000);
					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-01-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-03-31").getTime() / 1000);
				} else if (quarter == 2) {
					timeToolsBean.setBeforestarttime(formatter.parse(
							uptime + "-04-01").getTime() / 1000);
					timeToolsBean.setBeforeendtime(formatter.parse(
							uptime + "-06-30").getTime() / 1000);
					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-04-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-06-30").getTime() / 1000);
				} else if (quarter == 3) {
					timeToolsBean.setBeforestarttime(formatter.parse(
							uptime + "-07-01").getTime() / 1000);
					timeToolsBean.setBeforeendtime(formatter.parse(
							uptime + "-09-30").getTime() / 1000);
					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-07-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-09-30").getTime() / 1000);
				} else if (quarter == 4) {
					timeToolsBean.setBeforestarttime(formatter.parse(
							uptime + "-10-01").getTime() / 1000);
					timeToolsBean.setBeforeendtime(formatter.parse(
							uptime + "-12-31").getTime() / 1000);
					timeToolsBean.setNowstarttime(formatter.parse(
							stattimes[0] + "-10-01").getTime() / 1000);
					timeToolsBean.setNowendtime(formatter.parse(
							stattimes[0] + "-12-31").getTime() / 1000);
				}
			} else if (length == 7) {// 月

				Date date = formatter.parse(stattime + "-01");
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				timeToolsBean.setNowstarttime(date.getTime() / 1000);

				calendar.add(Calendar.MONTH, 1);
				calendar.add(Calendar.DAY_OF_YEAR, -1);
				timeToolsBean.setNowendtime(calendar.getTimeInMillis() / 1000);

				calendar.add(Calendar.MONTH, -12);
				calendar.set(Calendar.DATE,
						calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
				timeToolsBean
						.setBeforeendtime(calendar.getTimeInMillis() / 1000);

				calendar.add(Calendar.DAY_OF_YEAR, 1);
				calendar.add(Calendar.MONTH, -1);
				timeToolsBean
						.setBeforestarttime(calendar.getTimeInMillis() / 1000);

				timeToolsBean.setStattimedesc(stattimes[0] + "年" + stattimes[1]
						+ "月");

			} else if (length == 9) {// 周

				int week = Integer.parseInt(stattimes[2]);
				Calendar calendar = Calendar.getInstance();
				calendar.set(Integer.parseInt(stattimes[0]),
						Integer.parseInt(stattimes[1]) - 1, 1);

				calendar.add(Calendar.WEEK_OF_MONTH, week - 1);
				timeToolsBean.setNowstarttime(formatter.parse(
						formatter.format(calendar.getTime())).getTime() / 1000);
				timeToolsBean
						.setNowendtime(timeToolsBean.getNowstarttime() + 604800);

				timeToolsBean.setBeforestarttime(timeToolsBean
						.getNowstarttime() - 31536000);
				timeToolsBean.setBeforeendtime(timeToolsBean
						.getBeforestarttime() + 604800);

				timeToolsBean.setStattimedesc(stattimes[0] + "年" + stattimes[1]
						+ "月第" + stattimes[2] + "周");

			} else if (length == 10) {// 日

				timeToolsBean.setNowstarttime(formatter.parse(stattime)
						.getTime() / 1000);
				timeToolsBean
						.setNowendtime(timeToolsBean.getNowstarttime() + 86400);

				timeToolsBean.setBeforestarttime(timeToolsBean
						.getNowstarttime() - 31536000);
				timeToolsBean.setBeforeendtime(timeToolsBean
						.getBeforestarttime() + 86400);

				timeToolsBean.setStattimedesc(stattimes[0] + "年" + stattimes[1]
						+ "月" + stattimes[2] + "日");
			}

			return timeToolsBean;

		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 判断是否为闰年 
	 * @param year
	 * @return
	 */
	public static boolean runNian(int year)// 判断是否为闰年的方法
	{
		boolean t = false;
		if (year % 4 == 0) {
			if (year % 100 != 0) {
				t = true;
			} else if (year % 400 == 0) {
				t = true;
			}
		}
		return t;
	}

	/**
	 * 获得小时标记 
	 * @param key
	 * @return
	 */
	public static String getHourDesc(String key) {
		String hourDesc = hourdesc.get(key);
		return hourDesc == null ? key : hourDesc;
	}

	/**
	 * 环比时间处理
	 * 
	 * @param comparetime
	 * @return
	 */
	public static Long[] dealLRRCompareTime(String comparetime) {

		Long[] compareTimes = new Long[4];// {上次开始时间，上次结束时间，本次开始时间，本次结束时间}
		try {
			if (comparetime.length() > 4) {

				Date date = formatter.parse(comparetime + "-01");
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				compareTimes[2] = date.getTime() / 1000;

				calendar.add(Calendar.MONTH, 1);
				calendar.add(Calendar.DAY_OF_YEAR, -1);
				compareTimes[3] = calendar.getTimeInMillis() / 1000;

				calendar.add(Calendar.MONTH, -1);
				calendar.set(Calendar.DATE,
						calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
				compareTimes[1] = calendar.getTimeInMillis() / 1000;

				calendar.add(Calendar.DAY_OF_YEAR, 1);
				calendar.add(Calendar.MONTH, -1);
				compareTimes[0] = calendar.getTimeInMillis() / 1000;

			} else {
				String uptime = String
						.valueOf(Integer.parseInt(comparetime) - 1);
				compareTimes[0] = formatter.parse(uptime + "-01-01").getTime() / 1000;
				compareTimes[1] = formatter.parse(uptime + "-12-31").getTime() / 1000;
				compareTimes[2] = formatter.parse(comparetime + "-01-01")
						.getTime() / 1000;
				compareTimes[3] = formatter.parse(comparetime + "-12-31")
						.getTime() / 1000;
			}

			return compareTimes;
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 同比时间处理
	 * 
	 * @param comparetime
	 * @return
	 */
	public static Long[] dealYOYCompareTime(String comparetime)
			throws ParseException {

		Long[] compareTimes = new Long[4];// {上次开始时间，上次结束时间，本次开始时间，本次结束时间}
		try {
			Date date = formatter.parse(comparetime + "-01");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			compareTimes[2] = date.getTime() / 1000;

			calendar.add(Calendar.MONTH, 1);
			calendar.add(Calendar.DAY_OF_YEAR, -1);
			compareTimes[3] = calendar.getTimeInMillis() / 1000;

			calendar.add(Calendar.MONTH, -12);
			calendar.set(Calendar.DATE,
					calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
			compareTimes[1] = calendar.getTimeInMillis() / 1000;

			calendar.add(Calendar.DAY_OF_YEAR, 1);
			calendar.add(Calendar.MONTH, -1);
			compareTimes[0] = calendar.getTimeInMillis() / 1000;

			return compareTimes;
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取时间的串描述
	 * 
	 * @param time
	 *            时间值，是同一时间时的毫秒表示
	 * @param pattern
	 *            时间输出模式，如果为空那么默认输出为满足的yyyy-MM-dd HH:mm:ss.SSS<br>
	 *            <blockquote>
	 *            <table border=0 cellspacing=3 cellpadding=0 * summary="Examples of date and time patterns interpreted in the U.S. * locale">
	 *            <tr bgcolor="#ccccff">
	 *            <th align=left>模式串
	 *            <th align=left>输出结果
	 *            <tr>
	 *            <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code>
	 *            <td><code>2001.07.04 AD at 12:08:56 PDT</code>
	 *            <tr bgcolor="#eeeeff">
	 *            <td><code>"EEE, MMM d, ''yy"</code>
	 *            <td><code>Wed, Jul 4, '01</code>
	 *            <tr>
	 *            <td><code>"h:mm a"</code>
	 *            <td><code>12:08 PM</code>
	 *            <tr bgcolor="#eeeeff">
	 *            <td><code>"hh 'o''clock' a, zzzz"</code>
	 *            <td><code>12 o'clock PM, Pacific Daylight Time</code>
	 *            <tr>
	 *            <td><code>"K:mm a, z"</code>
	 *            <td><code>0:08 PM, PDT</code>
	 *            <tr bgcolor="#eeeeff">
	 *            <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
	 *            <td><code>02001.July.04 AD 12:08 PM</code>
	 *            <tr>
	 *            <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
	 *            <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>
	 *            <tr bgcolor="#eeeeff">
	 *            <td><code>"yyMMddHHmmssZ"</code>
	 *            <td><code>010704120856-0700</code>
	 *            <tr>
	 *            <td><code>"yyyy-MM-dd HH:mm:ss.SSS"</code>
	 *            <td><code>2001-07-04 12:08:56.235</code>
	 *            </table>
	 *            </blockquote>
	 * 
	 * @return 返回的时间串描述，决不为空
	 */
	public static String simpleDateFormat(long time, String pattern) {
		if (time < 0) {
			time = 0;
		}
		if (pattern == null) {
			pattern = "yyyy-MM-dd HH:mm:ss.SSS";
		}
		return new SimpleDateFormat(pattern).format(new Date(time));
	}

	/**
	 * 将日期按照样式格式化 
	 * @param time
	 * @param pattern
	 * @return
	 */
	public static String simpleDateFormat(Date time, String pattern) {
		if (time == null) {
			return null;
		}
		if (pattern == null) {
			pattern = "yyyy-MM-dd HH:mm:ss.SSS";
		}
		return new SimpleDateFormat(pattern).format(time);
	}
	/**
	 * 将日期转换为制定格式的字符串 
	 * @param date 日期
	 * @param pattern 
	 * 	格式样式是字符串format的形式，例如%1$tm-%1$te-%1$tY
	 * @return
	 */
	public static String stringDateFormat(Calendar date,String pattern){
		 return String.format(pattern,date); 

	}
	/**
	 * 日期类型转换为utc值 秒
	 * @param date
	 * @return
	 */
	public static long date2utc(Date date){
		return date.getTime()/1000;
	}
	
	/**
	 * 将utc秒转换为日期对象 
	 * @param utc
	 * @return utc所表示的日期
	 */
	public static Date utc2date(long utc){
		return new Date(utc*1000);
	}
	
	/**
	 * 根据日期串和格式串返回日期对象 ,yyyyMMdd HH:mm:ss
	 * @param strDate	日期字符串
	 * @param pattern	日期样式
	 * @return
	 */
	public static Date getDateByPattern(String strDate,String pattern){
		Date d = new Date();
		try {
			d = new SimpleDateFormat(pattern).parse(strDate);
		} catch (ParseException e) {
			log.error(String.format("解析日期格式错误,str=%s,pattern=%s ,将返回当前时间",strDate,pattern),e);
		}
		return d;
	}
	/**
	 * 获取日期中某个段的值 
	 * @param d
	 * @param field
	 * @return
	 */
	public static int getDateFieldByCalendar(Date d,int field){
		int ret = -1;
		try {
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			ret = c.get(field);
		} catch (Exception e) {
			log.error(String.format("解析日期格式错误,str=%s,pattern=%s ,将返回当前时间",d,field),e);
		}
		return ret;
	}
	
	/**
	 * 获取指定timeid的起始时间小时与分钟HHMM 
	 * @param timeId
	 * @param interval 间隔分钟
	 * @return
	 */
	public static String getHHMM(int timeId, double interval) {
		DecimalFormat deciformat = new DecimalFormat("00");
		String strPublishTime = deciformat.format((int)((timeId - 1) * interval / ONE_HOUR_MINUTES)) + ":" + deciformat.format((timeId - 1) * interval % ONE_HOUR_MINUTES);
		return strPublishTime;
	}

	/**
	 * 根据日期值获取指定时间的timeid
	 * 默认时间间隔的分钟
	 * @param date	指定的日期
	 * @return	日期时间所代表的timeid
	 */
	public static int getTimeIDByDate(Date date,double interval) {
		return getTimeIDByMillis(date.getTime(),interval);
	}

	/**
	 * 根据日期毫秒值获取指定时间的timeid，最后一个整5分钟�?
	 * 
	 * @param millis
	 *            日期毫秒
	 * @return timeid 0-288
	 */
	public static int getTimeIDByMillis(long millis,double interval) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(millis);
		int minutes = cal.get(Calendar.MINUTE);
		int hours = cal.get(Calendar.HOUR_OF_DAY);
		int second = cal.get(Calendar.SECOND);
		int timeid = (int) ((hours * ONE_HOUR_MINUTES*ONE_HOUR_MINUTES+ minutes*ONE_HOUR_MINUTES +second) / (interval*ONE_HOUR_MINUTES));
		
		return timeid;
	}
	
	/**
	 * 切断日期的小时部分 
	 * @param second 代表日期的秒
	 * @return	去掉小时的秒数
	 */
	public static long truncatTimeFromDate(long second){
		long day = (second+TimeZone.getDefault().getRawOffset()/1000)/86400;
		return (day * 86400 -TimeZone.getDefault().getRawOffset()/1000 );
	}
	/**
	 * 切断日期的小时部分 
	 * @param date 原始日期
	 * @return	去掉小时的日期
	 */
	public static Date truncatTimeFromDate(Date date){
		long second = truncatTimeFromDate(date.getTime()/1000);
		date.setTime(second * 1000);
		return date;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Calendar call = Calendar.getInstance();
//		call.setTimeInMillis(1268720708 * 1000);
		Date date = call.getTime();
		String sd = formatter.format(date);
	
		System.out.println("系统原始日期是"+stringDateFormat(call,"%1$tY-%1$tm-%1$te"));
		long utc = date2utc(date);
		System.out.println("date2utc时间秒数是"+utc);
		System.out.println("utc2date值是"+utc2date(utc));
		System.out.println("getDateByPattern值是"+getDateByPattern("20111031 12:32:34", "yyyyMMdd kk:mm:ss"));
		
		System.out.println("simpleDateFormat结果是"+simpleDateFormat(utc*1000,"yyyyMMdd"));
		System.out.println("simpleDateFormat2结果是"+simpleDateFormat(date,"yyyyMMdd"));
		
		System.out.println("getDateFieldByCalendar结果是"+getDateFieldByCalendar(date,Calendar.DAY_OF_WEEK));
		System.out.println("truncatTimeFromDate-utc结果是"+truncatTimeFromDate(utc));
		System.out.println("truncatTimeFromDate-date结果是"+truncatTimeFromDate(date));
	}

}
